export default {
    // 编码
    base64Encode(text: string) {
        return btoa(unescape(encodeURIComponent(text)));
    },

    // 解码
    base64Decode(encoded: string) {
        return decodeURIComponent(escape(atob(encoded)));
    },

    simpleEncrypt(text: string, shift = 3) {
        return text
            .split('')
            .map((char) => String.fromCharCode(char.charCodeAt(0) + shift))
            .join('');
    },

    simpleDecrypt(encrypted: string, shift = 3) {
        return encrypted
            .split('')
            .map((char) => String.fromCharCode(char.charCodeAt(0) - shift))
            .join('');
    },

    hexEncode(text: string) {
        return text
            .split('')
            .map((c) => c.charCodeAt(0).toString(16).padStart(2, '0'))
            .join('');
    },

    hexDecode(hex: any) {
        return hex
            .match(/.{1,2}/g)
            .map((h: any) => String.fromCharCode(parseInt(h, 16)))
            .join('');
    },

    enc(text: string) {
        const step1 = this.simpleEncrypt(text, 5);
        return this.base64Encode(step1);
    },

    dec(encoded: string) {
        const step1 = this.base64Decode(encoded);
        return this.simpleDecrypt(step1, 5);
    },
};
